﻿
var dataTA = [];
var taEditId = -1;


$(document).ready(function () {
    initTA();

    $('a#transport-a-modal-caller').fancybox({
        showCloseButton: false,
        fitToView: true,
        autoSize: true,
        closeClick: true,
        autoDimensions: false,
        width: 640,
        openEffect: 'none',
        padding: 0,
        onStart: function () {
            if (taEditId == -1)
                $("#transport-a-modal h1.header").text("Add Tranportation A");
            else
                $("#transport-a-modal h1.header").text("Edit Tranportation A");
        },
        onClosed: function () {
            // Reset
            taEditId = -1;
            $("span#vldTAAmount").hide();
        }
    });

    $("a#lnkTransportA").click(function () {
        resetTAModal();
        $('a#transport-a-modal-caller').click();

    });
});

function initTA() {
    var str = $("#txtTransportA").val();
    if (str != '') {
        dataTA = JSON.parse(str);
        for (var i = 0; i < dataTA.length; i++) {
            var item = dataTA[i];
            item.id = i;
        }
    }


    loadTAGrid();
}

function callbackFromTAModal(row) {
    processTAData(row);
}

function processTAData(row) {
    if (taEditId != -1) {

        for (var i = 0; i < dataTA.length; i++) {
            var item = dataTA[i];
            if (item.id == taEditId) {
                item.item_id = row.item_id;
                item.item_type = row.item_type;
                item.item_name = row.item_name;
                item.amount = row.amount;
                item.unit_id = row.unit_id;
                item.unit_name = row.unit_name;

                break;
            }
        }



    }
    else {
        var inputRow = {
            id: getNextId(dataTA),
            item_id: row.item_id,
            item_type: row.item_type,
            item_name: row.item_name,
            amount: row.amount,
            unit_id: row.unit_id,
            unit_name: row.unit_name
        };

        dataTA.push(inputRow);

    }


    updateTA();
    loadTAGrid();
    focusTAGrid();
}
function deleteTARow(id) {
    for (var i = 0; i < dataTA.length; i++) {
        var item = dataTA[i];
        if (item.id == id) {
            dataTA.splice(i, 1);
            break;
        }
    }
    updateTA();
    loadTAGrid();
    focusTAGrid();
}

function editTARow(id) {
    var selectedRow = null;
    for (var i = 0; i < dataTA.length; i++) {
        var item = dataTA[i];
        if (item.id == id) {
            selectedRow = item;
            break;
        }
    }
    taEditId = id;

    resetTAModal();
    loadTAModal(selectedRow);
    $('a#transport-a-modal-caller').click();
}

function updateTA() {
    $("#txtTransportA").val(JSON.stringify(dataTA));

}

function focusTAGrid() {
    $(window).scrollTop($('table#gridTransportA tr:last').offset().top);
}

function loadTAGrid() {
    $('table#gridTransportA').empty();

    var oTable = $('table#gridTransportA').dataTable({
        "bProcessing": true,
        "bDestroy": true,
        "bPaginate": false,
        "bAutoWidth": false,
        "bFilter": false,
        "aaSorting": [],
        "aaData": dataTA,
        "aoColumns": [

                    { "mDataProp": "item_name", "bSortable": true, "sTitle": "Name", "sClass": "cName", "bSearchable": true, "bVisible": true },
                    { "mDataProp": "amount", "bSortable": false, "sTitle": "Amount", "sClass": "cAmount", "bSearchable": true, "bVisible": true },
                    { "mDataProp": "unit_name", "bSortable": false, "sTitle": "Unit", "sClass": "cUnit", "bSearchable": true, "bVisible": true },
                    { "mDataProp": null, "bSortable": false, "sTitle": "", "sClass": "cDelete", "sDefaultContent": "<input id='btnEdit' class='button' type='button' value='Edit' /><input id='btnDelete' class='button' type='button' value='Delete' />", "bVisible": true }
                ],
        "fnCreatedRow": function (nRow, aData, iDataIndex) {
            // Assign id to 'tr'
            $(nRow).attr('id', aData.id);

            $('input#btnEdit', nRow).click(function (e) {
                editTARow(aData.id);
            });

            $('input#btnDelete', nRow).click(function (e) {

                if (confirm('Are you sure to delete this?')) {
                    deleteTARow(aData.id);
                }
            });
        }


    });
}